using System.Collections;
using UnityEngine;

public class HeliWeapons : MonoBehaviour
{
	public GameObject[] WeaponsPivot;

	public int Rockets;

	public int Bullets;

	public float RocketReloadTime;

	private float _curRocketReload;

	public float BulletReloadTime;

	private float _curBulletReload;

	private bool _rightRocketSide;

	private GameObject _heli;

	private void Awake()
	{
		_heli = base.transform.parent.gameObject;
	}

	private void Update()
	{
		if (_curBulletReload > 0f)
		{
			_curBulletReload -= Time.deltaTime;
		}
		if (_curRocketReload > 0f)
		{
			_curRocketReload -= Time.deltaTime;
		}
		if (!_heli.GetComponent<HeliController>().EnableControls)
		{
			return;
		}
		if (Input.GetAxis("Fire1") == 1f && Bullets > 0 && _curBulletReload <= 0f)
		{
			RaycastHit hitInfo;
			if (Physics.Raycast(WeaponsPivot[0].transform.position, WeaponsPivot[0].transform.forward, out hitInfo))
			{
				StartCoroutine(BulletFlight(hitInfo.point, hitInfo.collider.gameObject, Vector3.Distance(WeaponsPivot[0].transform.position, hitInfo.point) * 0.012f));
			}
			WeaponsPivot[0].GetComponent<AudioSource>().pitch = Random.Range(0.8f, 1.2f);
			WeaponsPivot[0].GetComponent<AudioSource>().Play();
			WeaponsPivot[0].GetComponent<ParticleSystem>().Emit(1);
			Bullets--;
			_curBulletReload = BulletReloadTime;
		}
		if (Input.GetAxis("Fire2") == 1f && Rockets > 0 && _curRocketReload <= 0f)
		{
			Transform transform;
			if (_rightRocketSide)
			{
				transform = WeaponsPivot[1].transform;
				WeaponsPivot[1].GetComponent<AudioSource>().pitch = Random.Range(0.9f, 1.1f);
				WeaponsPivot[1].GetComponent<AudioSource>().Play();
				WeaponsPivot[1].GetComponent<ParticleSystem>().Emit(1);
				_rightRocketSide = !_rightRocketSide;
			}
			else
			{
				transform = WeaponsPivot[2].transform;
				WeaponsPivot[2].GetComponent<AudioSource>().pitch = Random.Range(0.9f, 1.1f);
				WeaponsPivot[2].GetComponent<AudioSource>().Play();
				WeaponsPivot[2].GetComponent<ParticleSystem>().Emit(1);
				_rightRocketSide = !_rightRocketSide;
			}
			RaycastHit hitInfo2;
			if (Physics.Raycast(transform.position, transform.forward, out hitInfo2))
			{
				StartCoroutine(RocketFlight(hitInfo2.point, hitInfo2.collider.gameObject, Vector3.Distance(transform.position, hitInfo2.point) * 0.018f));
			}
			Rockets--;
			_curRocketReload = RocketReloadTime;
		}
	}

	private IEnumerator BulletFlight(Vector3 point, GameObject target, float time)
	{
		yield return new WaitForSeconds(time);
		if (target.GetComponent<Rigidbody>() != null)
		{
			target.GetComponent<Rigidbody>().AddForce(_heli.transform.forward * 300f);
		}
	}

	private IEnumerator RocketFlight(Vector3 point, GameObject target, float time)
	{
		yield return new WaitForSeconds(time);
		if (target.GetComponent<Rigidbody>() != null)
		{
			target.GetComponent<Rigidbody>().AddForce(_heli.transform.forward * 600f);
		}
		Collider[] colliders = Physics.OverlapSphere(point, 10f);
		Collider[] array = colliders;
		foreach (Collider hit in array)
		{
			if ((bool)hit.GetComponent<Rigidbody>())
			{
				hit.GetComponent<Rigidbody>().AddExplosionForce(300f, point, 10f, 3f);
			}
		}
	}
}
